﻿using System.Runtime.CompilerServices;

namespace KinonekoSoftware.CodariaDB.Runtimes.Categories
{
    [DebuggerDisplay("{Id}-{Name}")]
    public class KeywordCatalog : Catalog, IHierarchicalObject
    {

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsRoot() => string.IsNullOrEmpty(ParentID);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool HasParent() => !string.IsNullOrEmpty(ParentID);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetID() => Id;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetParentID() => ParentID;

        public void AppendChild(IHierarchicalObject instance)
        {
            var instance2 = (KeywordCatalog)instance;
            Children.Add(instance2);
        }
        
        /*
         * 在数据库中的结构：
         * 
         * KeywordCatalog : ID0001 - Root - nil
         * KeywordCatalog : ID0002 - A - ID0001 
         * KeywordCatalog : ID0003 - B - ID0001 
         * KeywordCatalog : ID0004 - C - ID0003
         * KeywordCatalog : ID0005 - D - ID0003
         *
         * 在内存中的结构:
         *
         *              Root
         *             /    \
         *           A       B
         *                 /   \
         *                C      D 
         */
        public string       ParentID      { get; set; }
        public int          Index         { get; set; }
        public InstanceType Type          { get; set; }
        public string       CustomType    { get; set; }
        public bool         UseCustomType { get; set; }
        
        [BsonIgnore]
        [JsonIgnore]
        private ObservableList<KeywordCatalog> _children;

        [BsonIgnore]
        [JsonIgnore]
        public ObservableList<KeywordCatalog> Children => _children ??= new ObservableList<KeywordCatalog>();
    }
}